<?php require_once('finance/inc/item_status.php'); ?>
<?php 

$auth = $_SESSION['auth'];
if (empty($auth)) { 
	header('Location:'.s_url('?c=user&a=login'));
	die;
}

if (empty($_GET)) die;

$page = @intval($_GET['page']);

if (empty($page)) { $page = 1; }
$limit = 5;
$user_id = $auth['id'];
$cond = "WHERE id in(SELECT item_id FROM finance_item_invest WHERE user_id = $user_id)";

$items = dt_query("SELECT id, name, money, revenue, term, invest, c_at FROM finance_item $cond ORDER BY c_at DESC LIMIT ".$limit * ($page - 1).",$limit");
if (!$items) {
	put_info('获取数据失败！');
	header('Location:?c=finance');
	die;
}

?>
<?php require_once('finance/inc/nav_sub_xm.html') ?>
<div class="container">
	<h3 style="font-size:18px">[<?php echo $auth['name'] ?>] 意向投资的项目</h3>
	<hr style="border-top:1px solid #ccc">
	<div class="row clearfix">
		<div class="col-md-9 column" style="border-right: 1px solid #ddd">
			<table class="table">
				<thead>
					<tr>
						<th>项目名称</th>
						<th>投资人</th>
						<th>融资额（元）</th>
						<th>年化收益率</th>
						<th>期限</th>
						<th>发布时间</th>
						<th>操作</th>
					</tr>
				</thead>
				<tbody>
				<?php while($item = mysql_fetch_array($items)) { ?>
				<tr id="<?php echo $item['id']; ?>">
					<td><a href="?c=finance&a=item_show&item_id=<?php echo $item['id']; ?>"><?php echo $item['name'] ?></a></td>
					<td><?php echo $item['invest'] ?></td>
					<td><?php echo $item['money'] ?></td>
					<td><?php echo $item['revenue'].'%' ?></td>
					<td><?php echo $item['term'],'个月' ?></td>
					<td><?php echo date('Y-m-d', $item['c_at']) ?></td>
					<td>
						<a class="btn btn-default btn-sm" href="javascript:get_item_edit_invest(<?php echo $item['id'] ?>)">修改意向</a>
						<a class="btn btn-default btn-sm" href="javascript:do_item_del_invest(<?php echo $item['id'] ?>)">撤销</a>
					</td>
				</tr>
				<?php } ?>
				</tbody>
			</table>

			<?php pagination('finance_item', $cond, $page, $limit, '?c=finance&a=item_my_invest&page=') ?>

		</div>

		<div class="col-md-3 column">
			<section>
				<h3 style="font-size:18px">投资概况</h3>
				<hr style="border-top:1px dashed #ccc">
				<table class="table">
					<thead>
						<tr>
							<th>状态</th>
							<th class="text-center">项目数</th>
							<th class="text-right">金额（元）</th>
						</tr>
					</thead>
					<tbody>
					<tr>
						<td><?php echo $item_status[FINANCE_ITEM_STATUS_TB]['name'] ?></td>
						<td class="text-center"><?php echo dt_count('finance_item', $cond." AND status = ".FINANCE_ITEM_STATUS_TB) ?></td>
						<td class="text-right"><?php echo dt_sum('finance_item', 'money', $cond." AND status = ".FINANCE_ITEM_STATUS_TB) ?></td>
					</tr>
					<tr>
						<td><?php echo $item_status[FINANCE_ITEM_STATUS_HK]['name'] ?></td>
						<td class="text-center"><?php echo dt_count('finance_item', $cond." AND status = ".FINANCE_ITEM_STATUS_HK) ?></td>
						<td class="text-right"><?php echo dt_sum('finance_item', 'money', $cond." AND status = ".FINANCE_ITEM_STATUS_HK) ?></td>
					</tr>
					<tr>
						<td><?php echo $item_status[FINANCE_ITEM_STATUS_JS]['name'] ?></td>
						<td class="text-center"><?php echo dt_count('finance_item', $cond." AND status = ".FINANCE_ITEM_STATUS_JS) ?></td>
						<td class="text-right"><?php echo dt_sum('finance_item', 'money', $cond." AND status = ".FINANCE_ITEM_STATUS_JS) ?></td>
					</tr>
					</tbody>
				</table>
			</section>
		</div>
	</div>
</div>

<div class="container" id="i_i_edit">
</div>

<script type="text/javascript">

$(document).ready(function(){
		$('li#finance_xm').addClass('active');
		$('li#xm_tzxm').addClass('active');
		});

function get_item_edit_invest(item_id) {
	$.get('?c=finance&a=get_item_edit_invest&item_id='+item_id, function(rs){
		if ('e0' == rs) { 
			alert(rs);
			return;
		} 
		$('div#i_i_edit').html(rs);
	});
}

function do_item_del_invest(item_id) {
	if (!confirm (" O_O 确定要撤销吗！")) return;
	
	$.get('?c=finance&a=do_item_del_invest&item_id='+item_id, function(rs){
		if ('s0' != rs) { 
			alert(rs);
			return;
		} 
		alert('成功撤销！');
		$('tr#'+item_id).remove();
		$('div#i_i_edit').html('');
	});
}


</script>
